﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PMS.Logic.PM
{
    public partial class BottlePrintAjax : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String methodName = Request["method"];
            Type type = this.GetType();
            MethodInfo method = type.GetMethod(methodName);
            if (method == null) throw new Exception("method is null");
            try
            {
                method.Invoke(this, null);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 通过ID获取打印主信息
        /// </summary>
        public void GetBottlePrintMainById()
        {
            //通过ID查询
            if (Request.Form["ID"] != null && Request.Form["ID"] != "")
            {
                int id = Commons.String2Int32(Request.Form["ID"]);
                var per = DALSession.Current.View_PM_BottleAnalyseListDAL.GetListBy(p => p.ID == id).FirstOrDefault();
                var quo = DALSession.Current.CM_QuotationDetailDAL.GetListBy(c => c.ID == per.QuotationDetailID).FirstOrDefault();
                Response.Write(JsonHelper.toJson(new { per = per, quo = quo }));
                return;
            }
        }


        /// <summary>
        /// 通过ID或获取气体打印明细
        /// </summary>
        public void GetBottlePrintDetailById()
        {
            //通过ID查询
            if (Request.Form["ID"] != null && Request.Form["ID"] != "")
            {
                int id = Commons.String2Int32(Request.Form["ID"]);
                var list = DALSession.Current.View_PM_BottleAnalyseDetailDAL.GetListBy(u => u.MainId == id).OrderBy(c => c.IsBottom).ThenBy(c => c.OperOrder).ToList();
                PMS.Logic.CM.QuotationAjax cq = new CM.QuotationAjax();
                foreach (var item in list)
                {
                    item.computeMGCM = cq.PPMToMGCMNoN(Commons.String2Double(item.Concentration) * 10000, Commons.String2Double(item.GasWeight));
                    item.actualMGCM = cq.PPMToMGCMNoN(Commons.String2Double(item.AnalyseConcentration) * 10000, Commons.String2Double(item.GasWeight));
                }
                Response.Write(JsonHelper.toJson(list));
                return;
            }
        }

        public void SavePrintLabelContent()
        {
            int ID = Commons.String2Int32(Request.Form["ID"]);
            string date = Request.Form["date"];
            string url = Request.Form["url"];
            string printColumn = Request.Form["printColumn"];
            string printChiOrEng = Request.Form["printChiOrEng"];
            string printIsAir = Request.Form["printIsAir"];
            PM_BottleProduction bottle = DALSession.Current.PM_BottleProductionDAL.GetListBy(d => d.ID == ID).FirstOrDefault();
            bottle.DeadLine = date;
            bottle.LabelUrl = url;
            bottle.PrintColumn = Commons.String2Int32(printColumn);
            bottle.PrintChiOrEng = printChiOrEng;
            bottle.PrintIsAir = printIsAir;
            if (bottle.PrintState == null || bottle.PrintState == "") bottle.PrintState = "0";
            //0 未打印，1 已标签，2 已证书，3 已标签已证书
            bottle.PrintState = bottle.PrintState == "0" || bottle.PrintState == "1" ? "1" : "3";

            DALSession.Current.PM_BottleProductionDAL.ModifyBy(bottle, d => d.ID == ID, "DeadLine", "LabelUrl", "PrintState", "printColumn", "printChiOrEng", "printIsAir");
            DALSession.Current.PM_BottleProductionDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "打印内容保存成功！"));
        }

        public void SavePrintCertificateContent()
        {
            int ID = Commons.String2Int32(Request.Form["ID"]);
            string url = Request.Form["url"];
            string gasName = Request.Form["GasName"];
            string deadline = Request.Form["deadLine"];
            string confirmDate = Request.Form["confirmDate"];
            PM_BottleProduction bottle = DALSession.Current.PM_BottleProductionDAL.GetListBy(d => d.ID == ID).FirstOrDefault();
            bottle.CetificateUrl = url;
            if (bottle.PrintState == null || bottle.PrintState == "") bottle.PrintState = "0";
            //0 未打印，1 已标签，2 已证书，3 已标签已证书
            bottle.PrintState = bottle.PrintState == "0" || bottle.PrintState == "2" ? "2" : "3";
            bottle.AnaMethod = gasName;
            bottle.PrintDeadline = deadline;
            bottle.ConfirmDate = confirmDate;
            DALSession.Current.PM_BottleProductionDAL.ModifyBy(bottle, d => d.ID == ID, "CetificateUrl", "PrintState", "AnaMethod", "PrintDeadline", "ConfirmDate");
            DALSession.Current.PM_BottleProductionDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "打印内容保存成功！"));
        }
    }
}